
export enum Actions {
    // value need to Lower Case
    None = 'none',
    ConnectWallet = 'connectwallet',
    WebView = 'webview'
}

export interface Protocol {
    protocol: string;
    action: Actions;
    params?: { [key: string]: any }
}

class RouterProtocol {
    static parse(str: string): Protocol | undefined {
        if (!str) {
            return
        }

        const idx = str.indexOf('://')
        if (idx === -1) {
            return
        }

        const protocol = str.substring(0, idx)
        if (str.startsWith('http')) {
            return {
                protocol,
                action: Actions.WebView
            }
        }

        const temp = str.replace(protocol, 'https')
        const url = new URL(temp)
        const action = url.hostname?.toLowerCase() as Actions
        const params: { [key: string]: any } = {}
        url.searchParams.forEach((v, k) => {
            params[k] = v
        })

        return {
            protocol,
            action,
            params
        }
    }
}

export default RouterProtocol